home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Adding Arrays, anyone?
- Date: 26 Jan 1996 15:38:03 GMT
- Organization: Los Alamos National Laboratory
- Distribution: world
- Message-ID: <TANMOY.96Jan26083803@qcd.lanl.gov>
- References: <8B97599.014F009D28.uuout@filebank.cts.com>
- <8B9852B.014F009DE3.uuout@filebank.cts.com>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: jack.greene@filebank.cts.com's message of Thu, 25 Jan 96 22:03:00 -0800
-
- In article <8B9852B.014F009DE3.uuout@filebank.cts.com>
- jack.greene@filebank.cts.com (JACK GREENE) writes:
- <snip>
- RK> I tried something like
- RK>
- RK> for(counter =0 ; counter != '\0'; counter++)
- RK> printf("%c",string1[counter] + string2[counter]
- RK>
- RK> and I got a bunch of garbage on the screen.
-
- :-) And I bet it didn't compile very well either :-)
-
- Why do you say that? The missing ); is obviously a typo: is that what
- you are talking about?
-
- When adding strings together like you've shown above, C will
- concatenate as opposed to doing the math that you're asking
-
- What do you mean by concatenate in this context? I can't see any
- concatenation going on there?
-
- it to do. Try converting to a numeric variable to
- add. I would try something like this:
-
- for (counter = 0; counter != '0'; counter++) {
- printf("%c", itoa(
- atoi( string1[counter] ) +
- atoi(string2[counter]) )
- };
-
- The functions:
- atoi() /* ascii to integer */
- itoa() /* integer to ascii */
-
- are in the STDLIB.H in borlands cpp4.51
-
- In every standard conforming compiler, <stdlib.h> has to declare
- atoi. In no standard conforming compiler can it declare itoa.
-
- atoi needs strings. I assume string1[counter] is a char, not a
- string. atoi cannot be legally passed a char (or int that results from
- the promotion of a char). Did you try your solution before posting?
-
- Also, atoi can be used to interpret character strings as numbers even
- on machines which do not use ascii.
-
- Of course somebody else might have it a little neater and I wouldn't
- mind seeing that either.
- The main idea is to convert the strings to integers. You could even
- change the printf() to use
- "%d" and not even bother with the itoa().
-
- Actually, I thought the question was of converting _characters_ to
- integers: not strings to integers. A string is a sequence of
- characters terminated by '\0', and it seemed the original poster
- wanted a sequence of integers (not one integer) from a sequence of
- characters.
-
- In any case, as digits are guaranteed to be contiguous in any code
- used by a C implementation, string1[counter]-'0' gives the int you
- want (provided of course string1[counter] is between '0' and
- '9'). Similarly, if x is any int between 0 and 9, '0'+x is the
- corresponding character.
-
- Just a thought from an old BASIC and xBase hack learning C myself.
-
- Happy hacking ...
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-